from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score
import pickle
from tqdm import tqdm
import matplotlib.pyplot as plt

# 加载手写数字数据集
digits = load_digits()
X = digits.data  # 特征数据
y = digits.target  # 标签数据

# 将数据集划分为训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42  # 测试集占20%，设置随机种子保证结果可复现
)

# 初始化变量以存储最佳准确率，相应的k值和最佳knn模型
best_accuracy = 0.0
best_k = 1
best_knn = None

# 初始化列表，分别存储k值和对应的准确率
k_values = []
accuracy_scores = []

# 使用tqdm创建进度条，遍历k值范围
for k in tqdm(range(1, 41), desc="寻找最优K值"):
    # 创建KNN分类器
    knn = KNeighborsClassifier(n_neighbors=k)
    
    # 训练模型
    knn.fit(X_train, y_train)
    
    # 预测测试集
    y_pred = knn.predict(X_test)
    
    # 计算准确率
    accuracy = accuracy_score(y_test, y_pred)
    k_values.append(k)
    accuracy_scores.append(accuracy)
    
    # 检查是否为最佳模型
    if accuracy > best_accuracy:
        best_accuracy = accuracy
        best_k = k
        best_knn = knn

# 将最佳KNN模型保存到二进制文件
with open('best_knn_model.pkl', 'wb') as f:
    pickle.dump(best_knn, f)

# 打印最佳准确率和相应的k值
print(f"\n最佳K值: {best_k}")
print(f"最佳准确率: {best_accuracy:.4f}")

# 绘制准确率随k值变化的折线图
plt.figure(figsize=(10, 6))
plt.plot(k_values, accuracy_scores, marker='o', linestyle='-', color='blue')
plt.axvline(x=best_k, color='red', linestyle='--')
plt.text(best_k + 0.5, best_accuracy, f'k={best_k}, Accuracy={best_accuracy:.4f}', color='red')
plt.xlabel('k value')
plt.ylabel('Accuracy')
plt.title('Accuracy of different k values')
plt.grid(True)
plt.savefig('accuracy_plot.pdf')
plt.show()
